﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using QMERP.Domain.Dtos;
using QMERP.Domain.Entities;
using QMERP.Infrastructure.CustomException;
using QMERP.Infrastructure.EFCore;
using QMERP.Infrastructure.EFCore.Extensions;
using QMERP.Infrastructure.Extensions;

namespace QMERP.Application.Services
{
    public class DictionaryCategoryService : IDictionaryCategoryService
    {
        private readonly QMERPDbContext _db;
        private readonly IMapper _mapper;
        public DictionaryCategoryService(QMERPDbContext db, IMapper mapper)
        { 
            _db = db;
            _mapper = mapper;
        }
        /// <summary>
        /// 字典类型页面
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<DictionaryCategoryResponse>> Query(PageRequest<DictionaryCategoryRequest> request)
        {
            var response = new PageResponse<DictionaryCategoryResponse>();
            var query = Where(request);
            var list = await query.Paging(request.PageIndex, request.PageSize).ToListAsync();
            response.TotalCount = await query.CountAsync();
            response.PageIndex = request.PageIndex;
            response.PageSize = request.PageSize;
            response.List = _mapper.Map<List<DictionaryCategoryResponse>>(list);
            return response;
        }
        /// <summary>
        /// 获取id的字典类型详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<DictionaryCategoryResponse> Detail(long id)
        {
            var model = await _db.DictionaryCategory.FindByIdAsync(id);
            return _mapper.Map<DictionaryCategoryResponse>(model);
        }
        /// <summary>
        /// 字典类型保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public async Task Save(DictionaryCategorySaveRequest request)
        {
            if (await _db.DictionaryCategory.AnyAsync(x => x.Category.ToLower() == request.Category && x.Id != request.Id))
                throw new ApiException("字典类型已存在!");
            if (await _db.DictionaryCategory.AnyAsync(x => x.Name == request.Name && x.Id != request.Id))
                throw new ApiException("字典类型名称已存在!");
            if (request.Id == null)
            {
                DictionaryCategory model = _mapper.Map<DictionaryCategory>(request);
                await _db.DictionaryCategory.AddAsync(model);
            }
            else
            {
                DictionaryCategory? model = await _db.DictionaryCategory.FindByIdAsync(request.Id.Value);
                if (model == null)
                    throw new ApiException("字典类型不存在!");
                model.Category = request.Category;
                model.Name = request.Name;
                model.Remark = request.Remark;
            }
            await _db.SaveChangesAsync();
        }
        private IQueryable<DictionaryCategory> Where(PageRequest<DictionaryCategoryRequest> request)
        {
            IQueryable<DictionaryCategory> query = _db.DictionaryCategory;
            if (!request.Query.Category.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Category.Contains(request.Query.Category!));
            }
            if (!request.Query.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Name.Contains(request.Query.Name!));
            }
            return query.OrderByDescending(x => x.CreatedOn);
        }
    }
}
